/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

/**
 * Compresses and decompresses string using <strong>LZW</strong> algo.
 * 
 * <p>Orignal ActionScript 2.0 implementation by Mika Palmu in his 
 * AsCrypt library, available in http://www.meychi.com/
 * 
 * <p>Original ActionScript port by Ash & Lalek & Shoebox and others :
 * 
 * @see "http://www.razorberry.com/blog/archives/2004/08/22/lzw-compression-methods-in-as2/"
 * @see "http://www.lalex.com/blog/comments/200405/164-compression-lzw-actionscript-2.html"
 * 
 * @author Romain Ecarnot
 */
class fever.data.compression.LZW
{
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Compresses and returns passed-in {@code source} using
	 * LZW algorithm.
	 */
	public static function compress( source : String ) : String
	{
		return _compress( source );
	}
	
	/**
	 * Decompresses and returns passed-in {@code source} using
	 * LZW algorithm.
	 */
	public static function decompress( source : String ) : String
	{
		return _decompress( source );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private static function _compress(source : String) : String
	{
		System.useCodepage = true;
		
		var chars : Number = 256;
		var original : String = source;
		var dict : Array = new Array();
		for (var i : Number = 0; i<chars; i++) dict[String(i)] = i;
		var result : String = new String("");
		var splitted : Array = original.split("");
		var buffer : Array = new Array();
		
		var xstr : String;
		
		for (var i : Number = 0; i<=splitted.length; i++) {
			var current : String = splitted[i];
			if (buffer.length == 0) xstr = String(current.charCodeAt(0));
			else xstr = buffer.join("-")+"-"+String(current.charCodeAt(0));
			
			if (dict[xstr] !== undefined){
				buffer.push(current.charCodeAt(0));
			} else {
				result += String.fromCharCode(dict[buffer.join("-")]);
				dict[xstr] = chars;
				chars++;
				delete buffer;
				buffer = new Array();
				buffer.push(current.charCodeAt(0));
			}
		}
		return result;
	}

	private static function _decompress(source : String) : String
	{
		var chars : Number = 256;
		var dict : Array = new Array();
		for (var i : Number = 0; i<chars; i++) {
			var c : String = String.fromCharCode(i);
			dict[i] = c;
		}
		var original : String = source;
		var splitted : Array = original.split("");
		var buffer : String = new String("");
		var chain : String = new String("");
		var result : String = new String("");
		
		for (var i : Number = 0; i<splitted.length; i++) {
			var code : Number = original.charCodeAt(i);
			var current : String = dict[code];
			if (buffer == "") {
				buffer = current;
				result += current;
			} else {
				if (code<=255) {
					result += current;
					chain = buffer+current;
					dict[chars] = chain;
					chars++;
					buffer = current;
				} else {
					chain = dict[code];
					if (chain == undefined) chain = buffer+buffer.slice(0,1);
					result += chain;
					dict[chars] = buffer+chain.slice(0,1);
					chars++;
					buffer = chain;
				}
			}
		}
		return result;
	}
}